<?php
$pageSize = 3;
$pageIndex = isset($_GET["pageIndex"])?$_GET["pageIndex"]:1;
$pageCount = 0;
$startRow = ($pageIndex-1)*$pageSize;
$sql = "select * from `article`";
$where = "";
$queryTitle = isset($_GET["Title"])?$_GET["Title"]:null;
if($queryTitle){
    $where .= "Title like ('%$queryTitle%')";
}

$queryAuthor = isset($_GET["Author"])?$_GET["Author"]:null;
if($queryAuthor){  
    if($where) $where .= " AND ";
    $where .= "AuthorName like ('%$queryAuthor%')";
}

$queryMinDate = isset($_GET["minDate"])?$_GET["minDate"]:null;
if($queryMinDate){
    if($where) $where .= " AND ";
    $where .= " CreateTime <='$queryMinDate'";
}

$queryMaxDate = isset($_GET["maxDate"])?$_GET["maxDate"]:null;
if($queryMaxDate){
    if($where) $where .= " AND ";
    $where .= " CreateTime >='$queryMaxDate'";
}
if($where) $sql .= "WHERE $where";
$conn = new mysqli("localhost","root","","myblog");
$rows = [];
$sql .= "LIMIT $startRow,$pageSize";
$rs = $conn->query($sql);
while($row = $rs->fetch_assoc()){
    $rows[] = $row;
}
$rs->close();

$total = 0;
$sql = "SELECT count(Id) as t FROM `article` ";
if($where) $sql .= " WHERE $where";
$rs = $conn->query($sql);
$total = $rs->fetch_assoc()["t"];
$pageCount = ceil($total/$pageSize);
function makePageUrl($pageIndex){
    $querystring = "pageIndex=$pageIndex";
    foreach($_GET as $k=>$v){
        if($k==="pageIndex") continue;
        $querystring .="&$k=$v";
    }
    return $querystring;
}

?>
<!DOCTYPE html>
<html>
        <head>
            <meta http-equiv="content-type" content="text/httml;charset=utf8"/>
            <title>第一个网页</title>
        </head>
        <body>
            <style type="text/css">
                *{
                    font-size:14px;
                    padding: 0;
                    margin: 0;
                }
                table{
                    border-collapse: collapse;
                    background-color: #eee;
                    border:1px soild #666;
                    width: 100%;
                }
                table td,table th{
                    padding: 6px;
                    border-collapse: collapse;
                    border:1px soild #ccc;
                }
                table th{
                    background-color: #333;
                    color: #eee;
                }
                table th div.title{
                    width: 400px;
                }
            </style>
            <h3>文章列表</h3>
            <a href="add_to.php">新建文章</a>
            <form action="" method="GET">
            <div>
                <label><div class="title">标题</div></label>
                <input type="text" name="Title" id="Title"  />
            </div>
            <div>
                <label>作者</label>
                <input type="text" name="Author" id="Title"  />
            </div>
            <div>
                <label>时间</label>
                <input type="text" name="minDate"  value="<?=$queryMinDate?>"  />
                <input type="text" name="maxDate"  value="<?=$queryMaxDate?>"  />
            </div>
                <input type="submit" name="submit" value="搜索"/>
            </form>
            <table border="1">
                <thead>
                    <tr>
                        <th>标题</th>
                        <th>作者</th>
                        <th>时间</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach($rows as $row){?>    
                <tr>
                    <td>
                     <?=$row["Title"]?> 
                    </td>
                    <td>
                    <?=$row["AuthorName"]?>
                    </td>
                    <td>
                    <?=$row["UpdateTime"]?><br/>
                    <?=$row["CreateTime"]?>
                    </td>
                    <td>
                        详细 修改 删除
                    </td>
                </tr>
                <?php } ?>
                </tbody>
                <tfoot>
                    <tr>
                        <td colspan="4">
                            共<?=$total ?>条记录，共<?=$pageCount ?>页当前为<input type='text' value="2" value="<?=$pageIndex?>"/>页,
                            <a href="article_list2.php?<?=makePageUrl(1)?>">首页</a> 
                            <
                                <?php for($i = 1;$i<=$pageCount;$i++) {?>
                                    <a href="article_list2.php<?=makePageUrl($i)?>"><?=$i ?></a>
                                <?php } ?>
                            >
                            <a href="article_list2.php<?=makePageUrl($pageCount)?>">尾页</a>
                        </td>
                    </tr>
                </tfoot>
            </table>
        </body>
<html>
